package org.sdkj.device.test.vein.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.sdkj.device.test.tcp.utils.DemoState;

public class FeatureUtils {

	/**
	 * 采集一个特征
	 * 
	 * @param recvBuf
	 */
	public static void addFeature(byte[] recvBuf) throws Exception {
		Map<String, Object> Feature = VeinUtils.img2Feature(recvBuf);
		byte[] feature = (byte[]) Feature.get("feature");
		System.out.println(feature.length);
		DemoState.features.add(feature);
	}

	/**
	 * 合并特征
	 */
	public static void mergeFeature() throws Exception {
		byte[] template = VeinUtils.toBigFeatrue(DemoState.features.get(0), DemoState.features.get(1),
				DemoState.features.get(2));
		System.out.println(template.length + "每个模板" + template.length / 3);
		DemoState.feature_template = template;
		
		try{
		//特征合并成功后把当前用户的指静脉数据存起来
			//DemoState.userVeinMap.put(DemoState.currentUserName, DemoState.feature_template);
			DemoState.userVeinMap.put(DemoState.currentUserName, template);
		}catch(Exception ex)
		{
			ex.printStackTrace();
			System.out.println("报错咯");
		}
	}

	/**
	 * 验证模板(1:N)
	 * 
	 * @param recvBuf
	 */
//	public static int checkFeature(byte[] recvBuf) throws Exception {
//		//调用C++库把采集到的图片变成特征
//		Map<String, Object> Feature = VeinUtils.img2Feature(recvBuf);
//		byte[] feature = (byte[]) Feature.get("feature");
//		List<byte[]> templates = new ArrayList<>();
//		templates.add(DemoState.feature_template);
//		int n = VeinUtils.search1_N(feature, templates);
//		return n;
//	}
	public static int checkFeature(byte[] recvBuf) throws Exception {
		//调用C++库把采集到的图片变成特征
		Map<String, Object> Feature = VeinUtils.img2Feature(recvBuf);
		byte[] feature = (byte[]) Feature.get("feature");
		List<byte[]> templates = new ArrayList<>();
		
		//临时存放key
		Map<Integer, String> mapKey = new HashMap<Integer, String>();
		
		 int i = 0;
		for(Map.Entry<String, byte[]> entry : DemoState.userVeinMap.entrySet()){
            System.out.println("key = " + entry.getKey() + ", value = " + entry.getValue());
            templates.add(entry.getValue());
            
            //将原来MAP的KEY放入新的MAP的VALUE 里面
            mapKey.put(i, entry.getKey());
            i++;
        }
		
		int n = VeinUtils.search1_N(feature, templates);
		
		
		System.out.println("n = " +n);
		System.out.println("name = " +mapKey.get(n-1));
		return n;
	}
	
}
